package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreCartDto;
import com.java110.intf.cart.IStoreCartInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户查询自己的购物车
 */
@Java110Cmd(serviceCode = "storeOrder.queryUserStoreCart")
public class QueryUserStoreCartCmd extends Cmd {

    @Autowired
    private IStoreCartInnerServiceSMO storeCartInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        //Assert.hasKeyAndValue(reqJson,"shopId","未包含商铺编号");
        super.validatePageInfo(reqJson);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String userId = CmdContextUtils.getUserId(context);
        StoreCartDto storeCartDto = BeanConvertUtil.covertBean(reqJson, StoreCartDto.class);
        storeCartDto.setPersonId(userId);
        int count = storeCartInnerServiceSMOImpl.queryStoreCartsCount(storeCartDto);

        List<StoreCartDto> storeCartDtos = null;
        if (count > 0) {
            storeCartDtos = storeCartInnerServiceSMOImpl.queryStoreCarts(storeCartDto);
            freshStoreCartDtos(storeCartDtos);
            freshShopName(storeCartDtos);
        } else {
            storeCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeCartDto.getRow()), count, storeCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    /**
     * 刷入店铺名称
     *
     * @param storeCartDtos
     */
    private void freshShopName(List<StoreCartDto> storeCartDtos) {
        if (ListUtil.isNull(storeCartDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();

        for (StoreCartDto tmpStoreCartDto : storeCartDtos) {
            shopIds.add(tmpStoreCartDto.getShopId());
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        for (StoreCartDto tmpStoreCartDto : storeCartDtos) {
            for (ShopDto tmpShopDto : shopDtos) {
                if (!tmpShopDto.getShopId().equals(tmpStoreCartDto.getShopId())) {
                    continue;
                }
                tmpStoreCartDto.setShopName(tmpShopDto.getShopName());
            }
        }

    }

    private void freshStoreCartDtos(List<StoreCartDto> storeCartDtos) {

        if (ListUtil.isNull(storeCartDtos)) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();
        List<String> modeIds = new ArrayList<>();

        for (StoreCartDto tmpProductDto : storeCartDtos) {
            productIds.add(tmpProductDto.getProductId());
            modeIds.add(tmpProductDto.getModeId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (StoreCartDto tmpProductDto : storeCartDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }
                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }

            tmpProductDto.setFeeModeType(MappingCache.getValue(DistributionModeDto.DOMAIN_AMLL, DistributionModeDto.FEE_MODE_TYPE));
        }
    }
}
